<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta name="theme-color" content="#222">



  
  
    
    
  <script src="/lib/pace/pace.min.js?v=1.0.2"></script>
  <link href="/lib/pace/pace-theme-minimal.min.css?v=1.0.2" rel="stylesheet">

  <script>
    (function(){
        if(''){
            if (prompt('请输入文章密码') !== ''){
                alert('密码错误！');
                history.back();
            }
        }
    })();
  </script>









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.3" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/favicon.ico?v=5.1.3">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon.ico?v=5.1.3">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon.ico?v=5.1.3">


  <link rel="mask-icon" href="/images/favicon.ico?v=5.1.3" color="#222">





  <meta name="keywords" content="Python,Face_recognition,人脸识别 - 手把手," />










<meta name="description" content="环境要求： Ubuntu17.10  Python 2.7.14  环境搭建：1. 安装 Ubuntu17.10  &amp;gt; 安装步骤在这里2. 安装 Python2.7.14 (Ubuntu17.10 默认Python版本为2.7.14)3. 安装 git 、cmake 、 python-pip1234567891011121314151617181920212223# 安装 git$ sudo">
<meta name="keywords" content="Python,Face_recognition,人脸识别 - 手把手">
<meta property="og:type" content="article">
<meta property="og:title" content="手把手教你用1行代码实现人脸识别 -- Python Face_recognition">
<meta property="og:url" content="http://www.kangvcar.com/手把手教你用1行代码实现人脸识别-Python-Face-recognition/index.html">
<meta property="og:site_name" content="Kangvcar | Blogs">
<meta property="og:description" content="环境要求： Ubuntu17.10  Python 2.7.14  环境搭建：1. 安装 Ubuntu17.10  &amp;gt; 安装步骤在这里2. 安装 Python2.7.14 (Ubuntu17.10 默认Python版本为2.7.14)3. 安装 git 、cmake 、 python-pip1234567891011121314151617181920212223# 安装 git$ sudo">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/2640591-6ad706a67e893fd7.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/600">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/2640591-db4c1cd2cb38bc82.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/600">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/2640591-5e60694d19c47531.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/600">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/2640591-56bda25aadb69c59.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/600">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/2640591-fbddbeb2b0f0031f.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/600">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/2640591-0f9e9d30ddfbfd95.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/600">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/2640591-6f559995643fb408.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/600">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/2640591-b1888ab0e011118c.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/600">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/2640591-fd063a1aaf489c93.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/600">
<meta property="og:updated_time" content="2017-12-30T12:42:57.656Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="手把手教你用1行代码实现人脸识别 -- Python Face_recognition">
<meta name="twitter:description" content="环境要求： Ubuntu17.10  Python 2.7.14  环境搭建：1. 安装 Ubuntu17.10  &amp;gt; 安装步骤在这里2. 安装 Python2.7.14 (Ubuntu17.10 默认Python版本为2.7.14)3. 安装 git 、cmake 、 python-pip1234567891011121314151617181920212223# 安装 git$ sudo">
<meta name="twitter:image" content="http://upload-images.jianshu.io/upload_images/2640591-6ad706a67e893fd7.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/600">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.3',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://www.kangvcar.com/手把手教你用1行代码实现人脸识别-Python-Face-recognition/"/>



  <script>
  (function(i,s,o,g,r,a,m){i["DaoVoiceObject"]=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;a.charset="utf-8";m.parentNode.insertBefore(a,m)})(window,document,"script",('https:' == document.location.protocol ? 'https:' : 'http:') + "//widget.daovoice.io/widget/0f81ff2f.js","daovoice")
  daovoice('init', {
      app_id: "f8921861"
    });
  daovoice('update');
  </script>





  <title>手把手教你用1行代码实现人脸识别 -- Python Face_recognition | Kangvcar | Blogs</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>
      <a href="https://github.com/kangvcar/kangvcar.github.io" class="github-corner" aria-label="View source on Github"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#FD6C6C; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>
<!--       <a href="https://github.com/kangvcar/kangvcar.github.io"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://camo.githubusercontent.com/365986a132ccd6a44c23a9169022c0b5c890c387/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f7265645f6161303030302e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png"></a>
 -->
    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Kangvcar | Blogs</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">知识的广度是深度的副产品</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off"
             placeholder="搜索..." spellcheck="false"
             type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://www.kangvcar.com/手把手教你用1行代码实现人脸识别-Python-Face-recognition/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Kangvcar">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/uploads/avatar.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Kangvcar | Blogs">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">手把手教你用1行代码实现人脸识别 -- Python Face_recognition</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-11-14T21:39:33+08:00">
                2017-11-14
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Python/" itemprop="url" rel="index">
                    <span itemprop="name">Python</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/手把手教你用1行代码实现人脸识别-Python-Face-recognition/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count"
                        data-disqus-identifier="手把手教你用1行代码实现人脸识别-Python-Face-recognition/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  1,443
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  7
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="环境要求："><a href="#环境要求：" class="headerlink" title="环境要求："></a>环境要求：</h1><ul>
<li><a href="http://ftp.sjtu.edu.cn/ubuntu-cd/17.10/ubuntu-17.10-desktop-amd64.iso" target="_blank" rel="external">Ubuntu17.10</a> </li>
<li><a href="https://www.python.org/downloads/release/python-2714" target="_blank" rel="external">Python 2.7.14</a></li>
</ul>
<h1 id="环境搭建："><a href="#环境搭建：" class="headerlink" title="环境搭建："></a>环境搭建：</h1><h5 id="1-安装-Ubuntu17-10-gt-安装步骤在这里"><a href="#1-安装-Ubuntu17-10-gt-安装步骤在这里" class="headerlink" title="1. 安装 Ubuntu17.10  &gt; 安装步骤在这里"></a>1. 安装 <a href="http://ftp.sjtu.edu.cn/ubuntu-cd/17.10/ubuntu-17.10-desktop-amd64.iso" target="_blank" rel="external">Ubuntu17.10</a>  &gt; 安装步骤在<a href="http://www.jianshu.com/p/778e92eb0461" target="_blank" rel="external">这里</a></h5><h5 id="2-安装-Python2-7-14-Ubuntu17-10-默认Python版本为2-7-14"><a href="#2-安装-Python2-7-14-Ubuntu17-10-默认Python版本为2-7-14" class="headerlink" title="2. 安装 Python2.7.14 (Ubuntu17.10 默认Python版本为2.7.14)"></a>2. 安装 Python2.7.14 (Ubuntu17.10 默认Python版本为2.7.14)</h5><h5 id="3-安装-git-、cmake-、-python-pip"><a href="#3-安装-git-、cmake-、-python-pip" class="headerlink" title="3. 安装 git 、cmake 、 python-pip"></a>3. 安装 git 、cmake 、 python-pip</h5><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 安装 git</span></span><br><span class="line">$ sudo apt-get install -y git</span><br><span class="line"><span class="comment"># 安装 cmake</span></span><br><span class="line">$ sudo apt-get install -y cmake</span><br><span class="line"><span class="comment"># 安装 python-pip</span></span><br><span class="line">$ sudo apt-get install -y python-pip</span><br><span class="line">``` </span><br><span class="line"><span class="comment">##### 4. 安装编译dlib </span></span><br><span class="line">安装face_recognition这个之前需要先安装编译dlib</span><br><span class="line">```python</span><br><span class="line"><span class="comment"># 编译dlib前先安装 boost</span></span><br><span class="line">$ sudo apt-get install libboost-all-dev</span><br><span class="line"></span><br><span class="line"><span class="comment"># 开始编译dlib</span></span><br><span class="line"><span class="comment"># 克隆dlib源代码</span></span><br><span class="line">$ git clone https://github.com/davisking/dlib.git</span><br><span class="line">$ cd dlib</span><br><span class="line">$ mkdir build</span><br><span class="line">$ cd build</span><br><span class="line">$ cmake .. -DDLIB_USE_CUDA=<span class="number">0</span> -DUSE_AVX_INSTRUCTIONS=<span class="number">1</span></span><br><span class="line">$ cmake --build .（注意中间有个空格）</span><br><span class="line">$ cd ..</span><br><span class="line">$ python setup.py install --yes USE_AVX_INSTRUCTIONS --no DLIB_USE_CUDA</span><br></pre></td></tr></table></figure>
<h5 id="5-安装-face-recognition"><a href="#5-安装-face-recognition" class="headerlink" title="5. 安装 face_recognition"></a>5. 安装 face_recognition</h5><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 安装 face_recognition</span></span><br><span class="line">$ pip install face_recognition</span><br><span class="line"><span class="comment"># 安装face_recognition过程中会自动安装 numpy、scipy 等</span></span><br></pre></td></tr></table></figure>
<p><img src="http://upload-images.jianshu.io/upload_images/2640591-6ad706a67e893fd7.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/600" alt="环境搭建完成后，在终端输入 face_recognition 命令查看是否成功 "></p>
<h1 id="实现人脸识别："><a href="#实现人脸识别：" class="headerlink" title="实现人脸识别："></a>实现人脸识别：</h1><hr>
<h3 id="示例一-1行代码实现人脸识别-："><a href="#示例一-1行代码实现人脸识别-：" class="headerlink" title="示例一(1行代码实现人脸识别)："></a>示例一(1行代码实现人脸识别)：</h3><h5 id="1-首先你需要提供一个文件夹，里面是所有你希望系统认识的人的图片。其中每个人一张图片，图片以人的名字命名："><a href="#1-首先你需要提供一个文件夹，里面是所有你希望系统认识的人的图片。其中每个人一张图片，图片以人的名字命名：" class="headerlink" title="1. 首先你需要提供一个文件夹，里面是所有你希望系统认识的人的图片。其中每个人一张图片，图片以人的名字命名："></a>1. 首先你需要提供一个文件夹，里面是所有你希望系统认识的人的图片。其中每个人一张图片，图片以人的名字命名：</h5><p><img src="http://upload-images.jianshu.io/upload_images/2640591-db4c1cd2cb38bc82.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/600" alt="known_people文件夹下有babe、成龙、容祖儿的照片"></p>
<h5 id="2-接下来，你需要准备另一个文件夹，里面是你要识别的图片："><a href="#2-接下来，你需要准备另一个文件夹，里面是你要识别的图片：" class="headerlink" title="2. 接下来，你需要准备另一个文件夹，里面是你要识别的图片："></a>2. 接下来，你需要准备另一个文件夹，里面是你要识别的图片：</h5><p><img src="http://upload-images.jianshu.io/upload_images/2640591-5e60694d19c47531.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/600" alt="unknown_pic文件夹下是要识别的图片，其中韩红是机器不认识的"></p>
<h5 id="3-然后你就可以运行face-recognition命令了，把刚刚准备的两个文件夹作为参数传入，命令就会返回需要识别的图片中都出现了谁："><a href="#3-然后你就可以运行face-recognition命令了，把刚刚准备的两个文件夹作为参数传入，命令就会返回需要识别的图片中都出现了谁：" class="headerlink" title="3. 然后你就可以运行face_recognition命令了，把刚刚准备的两个文件夹作为参数传入，命令就会返回需要识别的图片中都出现了谁："></a>3. 然后你就可以运行face_recognition命令了，把刚刚准备的两个文件夹作为参数传入，命令就会返回需要识别的图片中都出现了谁：</h5><p><img src="http://upload-images.jianshu.io/upload_images/2640591-56bda25aadb69c59.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/600" alt="识别成功！！！"></p>
<hr>
<h3 id="示例二-识别图片中的所有人脸并显示出来-："><a href="#示例二-识别图片中的所有人脸并显示出来-：" class="headerlink" title="示例二(识别图片中的所有人脸并显示出来)："></a>示例二(识别图片中的所有人脸并显示出来)：</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># filename : find_faces_in_picture.py</span></span><br><span class="line"><span class="comment"># -*- coding: utf-8 -*-</span></span><br><span class="line"><span class="comment"># 导入pil模块 ，可用命令安装 apt-get install python-Imaging</span></span><br><span class="line"><span class="keyword">from</span> PIL <span class="keyword">import</span> Image</span><br><span class="line"><span class="comment"># 导入face_recogntion模块，可用命令安装 pip install face_recognition</span></span><br><span class="line"><span class="keyword">import</span> face_recognition</span><br><span class="line"></span><br><span class="line"><span class="comment"># 将jpg文件加载到numpy 数组中</span></span><br><span class="line">image = face_recognition.load_image_file(<span class="string">"/opt/face/unknown_pic/all_star.jpg"</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 使用默认的给予HOG模型查找图像中所有人脸</span></span><br><span class="line"><span class="comment"># 这个方法已经相当准确了，但还是不如CNN模型那么准确，因为没有使用GPU加速</span></span><br><span class="line"><span class="comment"># 另请参见: find_faces_in_picture_cnn.py</span></span><br><span class="line">face_locations = face_recognition.face_locations(image)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 使用CNN模型</span></span><br><span class="line"><span class="comment"># face_locations = face_recognition.face_locations(image, number_of_times_to_upsample=0, model="cnn")</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 打印：我从图片中找到了 多少 张人脸</span></span><br><span class="line">print(<span class="string">"I found &#123;&#125; face(s) in this photograph."</span>.format(len(face_locations)))</span><br><span class="line"></span><br><span class="line"><span class="comment"># 循环找到的所有人脸</span></span><br><span class="line"><span class="keyword">for</span> face_location <span class="keyword">in</span> face_locations:</span><br><span class="line"></span><br><span class="line">        <span class="comment"># 打印每张脸的位置信息</span></span><br><span class="line">        top, right, bottom, left = face_location</span><br><span class="line">        print(<span class="string">"A face is located at pixel location Top: &#123;&#125;, Left: &#123;&#125;, Bottom: &#123;&#125;, Right: &#123;&#125;"</span>.format(top, left, bottom, right))</span><br><span class="line"></span><br><span class="line">        <span class="comment"># 指定人脸的位置信息，然后显示人脸图片</span></span><br><span class="line">        face_image = image[top:bottom, left:right]</span><br><span class="line">        pil_image = Image.fromarray(face_image)</span><br><span class="line">        pil_image.show()</span><br></pre></td></tr></table></figure>
<p><img src="http://upload-images.jianshu.io/upload_images/2640591-fbddbeb2b0f0031f.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/600" alt="用于识别的图片"></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 执行python文件</span></span><br><span class="line">$ python find_faces_in_picture.py</span><br></pre></td></tr></table></figure>
<p><img src="http://upload-images.jianshu.io/upload_images/2640591-0f9e9d30ddfbfd95.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/600" alt="从图片中识别出7张人脸，并显示出来"></p>
<hr>
<h3 id="示例三-自动识别人脸特征-："><a href="#示例三-自动识别人脸特征-：" class="headerlink" title="示例三(自动识别人脸特征)："></a>示例三(自动识别人脸特征)：</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># filename : find_facial_features_in_picture.py</span></span><br><span class="line"><span class="comment"># -*- coding: utf-8 -*-</span></span><br><span class="line"><span class="comment"># 导入pil模块 ，可用命令安装 apt-get install python-Imaging</span></span><br><span class="line"><span class="keyword">from</span> PIL <span class="keyword">import</span> Image, ImageDraw</span><br><span class="line"><span class="comment"># 导入face_recogntion模块，可用命令安装 pip install face_recognition</span></span><br><span class="line"><span class="keyword">import</span> face_recognition</span><br><span class="line"></span><br><span class="line"><span class="comment"># 将jpg文件加载到numpy 数组中</span></span><br><span class="line">image = face_recognition.load_image_file(<span class="string">"biden.jpg"</span>)</span><br><span class="line"></span><br><span class="line">＃查找图像中所有面部的所有面部特征</span><br><span class="line">face_landmarks_list = face_recognition.face_landmarks(image)</span><br><span class="line"></span><br><span class="line">print(<span class="string">"I found &#123;&#125; face(s) in this photograph."</span>.format(len(face_landmarks_list)))</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> face_landmarks <span class="keyword">in</span> face_landmarks_list:</span><br><span class="line"></span><br><span class="line">   ＃打印此图像中每个面部特征的位置</span><br><span class="line">    facial_features = [</span><br><span class="line">        <span class="string">'chin'</span>,</span><br><span class="line">        <span class="string">'left_eyebrow'</span>,</span><br><span class="line">        <span class="string">'right_eyebrow'</span>,</span><br><span class="line">        <span class="string">'nose_bridge'</span>,</span><br><span class="line">        <span class="string">'nose_tip'</span>,</span><br><span class="line">        <span class="string">'left_eye'</span>,</span><br><span class="line">        <span class="string">'right_eye'</span>,</span><br><span class="line">        <span class="string">'top_lip'</span>,</span><br><span class="line">        <span class="string">'bottom_lip'</span></span><br><span class="line">    ]</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> facial_feature <span class="keyword">in</span> facial_features:</span><br><span class="line">        print(<span class="string">"The &#123;&#125; in this face has the following points: &#123;&#125;"</span>.format(facial_feature, face_landmarks[facial_feature]))</span><br><span class="line"></span><br><span class="line">   ＃让我们在图像中描绘出每个人脸特征！</span><br><span class="line">    pil_image = Image.fromarray(image)</span><br><span class="line">    d = ImageDraw.Draw(pil_image)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> facial_feature <span class="keyword">in</span> facial_features:</span><br><span class="line">        d.line(face_landmarks[facial_feature], width=<span class="number">5</span>)</span><br><span class="line"></span><br><span class="line">    pil_image.show()</span><br></pre></td></tr></table></figure>
<p><img src="http://upload-images.jianshu.io/upload_images/2640591-6f559995643fb408.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/600" alt="自动识别出人脸特征"></p>
<hr>
<h3 id="示例四-识别人脸鉴定是哪个人-："><a href="#示例四-识别人脸鉴定是哪个人-：" class="headerlink" title="示例四(识别人脸鉴定是哪个人)："></a>示例四(识别人脸鉴定是哪个人)：</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># filename : recognize_faces_in_pictures.py</span></span><br><span class="line"><span class="comment"># -*- conding: utf-8 -*-</span></span><br><span class="line"><span class="comment"># 导入face_recogntion模块，可用命令安装 pip install face_recognition</span></span><br><span class="line"><span class="keyword">import</span> face_recognition</span><br><span class="line"></span><br><span class="line">＃将jpg文件加载到numpy数组中</span><br><span class="line">babe_image = face_recognition.load_image_file(<span class="string">"/opt/face/known_people/babe.jpeg"</span>)</span><br><span class="line">Rong_zhu_er_image = face_recognition.load_image_file(<span class="string">"/opt/face/known_people/Rong zhu er.jpg"</span>)</span><br><span class="line">unknown_image = face_recognition.load_image_file(<span class="string">"/opt/face/unknown_pic/babe2.jpg"</span>)</span><br><span class="line"></span><br><span class="line">＃获取每个图像文件中每个面部的面部编码</span><br><span class="line">＃由于每个图像中可能有多个面，所以返回一个编码列表。</span><br><span class="line">＃但是由于我知道每个图像只有一个脸，我只关心每个图像中的第一个编码，所以我取索引<span class="number">0</span>。</span><br><span class="line">babe_face_encoding = face_recognition.face_encodings(babe_image)[<span class="number">0</span>]</span><br><span class="line">Rong_zhu_er_face_encoding = face_recognition.face_encodings(Rong_zhu_er_image)[<span class="number">0</span>]</span><br><span class="line">unknown_face_encoding = face_recognition.face_encodings(unknown_image)[<span class="number">0</span>]</span><br><span class="line"></span><br><span class="line">known_faces = [</span><br><span class="line">    babe_face_encoding,</span><br><span class="line">    Rong_zhu_er_face_encoding</span><br><span class="line">]</span><br><span class="line"></span><br><span class="line">＃结果是<span class="keyword">True</span>/false的数组，未知面孔known_faces阵列中的任何人相匹配的结果</span><br><span class="line">results = face_recognition.compare_faces(known_faces, unknown_face_encoding)</span><br><span class="line"></span><br><span class="line">print(<span class="string">"这个未知面孔是 Babe 吗? &#123;&#125;"</span>.format(results[<span class="number">0</span>]))</span><br><span class="line">print(<span class="string">"这个未知面孔是 容祖儿 吗? &#123;&#125;"</span>.format(results[<span class="number">1</span>]))</span><br><span class="line">print(<span class="string">"这个未知面孔是 我们从未见过的新面孔吗? &#123;&#125;"</span>.format(<span class="keyword">not</span> <span class="keyword">True</span> <span class="keyword">in</span> results))</span><br></pre></td></tr></table></figure>
<p><img src="http://upload-images.jianshu.io/upload_images/2640591-b1888ab0e011118c.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/600" alt="显示结果如图"></p>
<hr>
<h3 id="示例五-识别人脸特征并美颜-："><a href="#示例五-识别人脸特征并美颜-：" class="headerlink" title="示例五(识别人脸特征并美颜)："></a>示例五(识别人脸特征并美颜)：</h3><figure class="highlight arduino"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"># filename : digital_makeup.py</span></span><br><span class="line"># -*- coding: utf<span class="number">-8</span> -*-</span><br><span class="line"># 导入pil模块 ，可用命令安装 apt-<span class="built_in">get</span> install python-Imaging</span><br><span class="line">from PIL <span class="keyword">import</span> Image, ImageDraw</span><br><span class="line"># 导入face_recogntion模块，可用命令安装 pip install face_recognition</span><br><span class="line"><span class="keyword">import</span> face_recognition</span><br><span class="line"></span><br><span class="line">＃将jpg文件加载到numpy数组中</span><br><span class="line"><span class="built_in">image</span> = face_recognition.load_image_file(<span class="string">"biden.jpg"</span>)</span><br><span class="line"></span><br><span class="line">＃查找图像中所有面部的所有面部特征</span><br><span class="line">face_landmarks_list = face_recognition.face_landmarks(<span class="built_in">image</span>)</span><br><span class="line"></span><br><span class="line"><span class="built_in">for</span> face_landmarks in face_landmarks_list:</span><br><span class="line">    pil_image = Image.fromarray(<span class="built_in">image</span>)</span><br><span class="line">    d = ImageDraw.Draw(pil_image, <span class="string">'RGBA'</span>)</span><br><span class="line"></span><br><span class="line">    ＃让眉毛变成了一场噩梦</span><br><span class="line">    d.polygon(face_landmarks[<span class="string">'left_eyebrow'</span>], <span class="built_in">fill</span>=(<span class="number">68</span>, <span class="number">54</span>, <span class="number">39</span>, <span class="number">128</span>))</span><br><span class="line">    d.polygon(face_landmarks[<span class="string">'right_eyebrow'</span>], <span class="built_in">fill</span>=(<span class="number">68</span>, <span class="number">54</span>, <span class="number">39</span>, <span class="number">128</span>))</span><br><span class="line">    d.<span class="built_in">line</span>(face_landmarks[<span class="string">'left_eyebrow'</span>], <span class="built_in">fill</span>=(<span class="number">68</span>, <span class="number">54</span>, <span class="number">39</span>, <span class="number">150</span>), <span class="built_in">width</span>=<span class="number">5</span>)</span><br><span class="line">    d.<span class="built_in">line</span>(face_landmarks[<span class="string">'right_eyebrow'</span>], <span class="built_in">fill</span>=(<span class="number">68</span>, <span class="number">54</span>, <span class="number">39</span>, <span class="number">150</span>), <span class="built_in">width</span>=<span class="number">5</span>)</span><br><span class="line"></span><br><span class="line">    ＃光泽的嘴唇</span><br><span class="line">    d.polygon(face_landmarks[<span class="string">'top_lip'</span>], <span class="built_in">fill</span>=(<span class="number">150</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">128</span>))</span><br><span class="line">    d.polygon(face_landmarks[<span class="string">'bottom_lip'</span>], <span class="built_in">fill</span>=(<span class="number">150</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">128</span>))</span><br><span class="line">    d.<span class="built_in">line</span>(face_landmarks[<span class="string">'top_lip'</span>], <span class="built_in">fill</span>=(<span class="number">150</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">64</span>), <span class="built_in">width</span>=<span class="number">8</span>)</span><br><span class="line">    d.<span class="built_in">line</span>(face_landmarks[<span class="string">'bottom_lip'</span>], <span class="built_in">fill</span>=(<span class="number">150</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">64</span>), <span class="built_in">width</span>=<span class="number">8</span>)</span><br><span class="line"></span><br><span class="line">    ＃闪耀眼睛</span><br><span class="line">    d.polygon(face_landmarks[<span class="string">'left_eye'</span>], <span class="built_in">fill</span>=(<span class="number">255</span>, <span class="number">255</span>, <span class="number">255</span>, <span class="number">30</span>))</span><br><span class="line">    d.polygon(face_landmarks[<span class="string">'right_eye'</span>], <span class="built_in">fill</span>=(<span class="number">255</span>, <span class="number">255</span>, <span class="number">255</span>, <span class="number">30</span>))</span><br><span class="line"></span><br><span class="line">    ＃涂一些眼线</span><br><span class="line">    d.<span class="built_in">line</span>(face_landmarks[<span class="string">'left_eye'</span>] + [face_landmarks[<span class="string">'left_eye'</span>][<span class="number">0</span>]], <span class="built_in">fill</span>=(<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">110</span>), <span class="built_in">width</span>=<span class="number">6</span>)</span><br><span class="line">    d.<span class="built_in">line</span>(face_landmarks[<span class="string">'right_eye'</span>] + [face_landmarks[<span class="string">'right_eye'</span>][<span class="number">0</span>]], <span class="built_in">fill</span>=(<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">110</span>), <span class="built_in">width</span>=<span class="number">6</span>)</span><br><span class="line"></span><br><span class="line">    pil_image.show()</span><br></pre></td></tr></table></figure>
<p><img src="http://upload-images.jianshu.io/upload_images/2640591-fd063a1aaf489c93.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/600" alt="美颜前后对比"></p>
<hr>

      
    </div>
    
    
    


  


    

    
      <div>
        <div align="center">
 
   <div class="">
   <p>&nbsp</p>
   <p>&nbsp</p>
   <p><span>
   <b>本文地址：</b><a href="/手把手教你用1行代码实现人脸识别-Python-Face-recognition/" title="手把手教你用1行代码实现人脸识别 -- Python Face_recognition">http://www.kangvcar.com/手把手教你用1行代码实现人脸识别-Python-Face-recognition/</a><br/><b>转载请注明出处，谢谢！</b>
   </span></p>
   </div>
 
</div>


<div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>坚持原创技术分享，随意打赏！您的支持将鼓励我继续创作！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/wechatpay.jpg" alt="Kangvcar 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/images/alipay.jpg" alt="Kangvcar 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <div>
      
        <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------感谢您的阅读-------------</div>
    
</div>
      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Python/" rel="tag"><i class="fa fa-tag"></i> Python</a>
          
            <a href="/tags/Face-recognition/" rel="tag"><i class="fa fa-tag"></i> Face_recognition</a>
          
            <a href="/tags/人脸识别-手把手/" rel="tag"><i class="fa fa-tag"></i> 人脸识别 - 手把手</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/手把手教你用7行代码实现微信聊天机器人-Python-wxpy/" rel="next" title="手把手教你用7行代码实现微信聊天机器人 -- Python wxpy">
                <i class="fa fa-chevron-left"></i> 手把手教你用7行代码实现微信聊天机器人 -- Python wxpy
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/face-recognition中文文档-第1节-人脸识别/" rel="prev" title="[face_recognition中文文档] 第1节 人脸识别">
                [face_recognition中文文档] 第1节 人脸识别 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
        <!-- JiaThis Button BEGIN -->
<div class="jiathis_style">
<span class="jiathis_txt">分享到：</span>
<a class="jiathis_button_fav">收藏夹</a>
<a class="jiathis_button_copy">复制网址</a>
<a class="jiathis_button_email">邮件</a>
<a class="jiathis_button_weixin">微信</a>
<a class="jiathis_button_qzone">QQ空间</a>
<a class="jiathis_button_tqq">腾讯微博</a>
<a class="jiathis_button_douban">豆瓣</a>
<a class="jiathis_button_share">一键分享</a>

<a href="http://www.jiathis.com/share?uid=2140465" class="jiathis jiathis_txt jiathis_separator jtico jtico_jiathis" target="_blank">更多</a>
<a class="jiathis_counter_style"></a>
</div>
<script type="text/javascript" >
var jiathis_config={
  data_track_clickback:true,
  summary:"",
  shortUrl:false,
  hideMore:false
}
</script>
<script type="text/javascript" src="http://v3.jiathis.com/code/jia.js?uid=" charset="utf-8"></script>
<!-- JiaThis Button END -->
      
    </div>
  </div>


          </div>
          


          

  
      <div id="lv-container" data-id="city" data-uid="MTAyMC8zMTgzNi84NDAw"></div>


  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/uploads/avatar.png"
                alt="Kangvcar" />
            
              <p class="site-author-name" itemprop="name">Kangvcar</p>
              <p class="site-description motion-element" itemprop="description">知识的广度是深度的副产品</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">28</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">13</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">32</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          <div class="links-of-author motion-element">
            
              
                <span class="links-of-author-item">
                  <a href="https://github.com/kangvcar" target="_blank" title="GitHub">
                    
                      <i class="fa fa-fw fa-github"></i>GitHub</a>
                </span>
              
                <span class="links-of-author-item">
                  <a href="http://cv.kangvcar.com" target="_blank" title="我的简历">
                    
                      <i class="fa fa-fw fa-user"></i>我的简历</a>
                </span>
              
                <span class="links-of-author-item">
                  <a href="https://twitter.com/kangvcar" target="_blank" title="Twitter">
                    
                      <i class="fa fa-fw fa-twitter"></i>Twitter</a>
                </span>
              
                <span class="links-of-author-item">
                  <a href="http://www.jianshu.com/u/d9c480744afd" target="_blank" title="简书">
                    
                      <i class="fa fa-fw fa-globe"></i>简书</a>
                </span>
              
                <span class="links-of-author-item">
                  <a href="http://blog.csdn.net/kangvcar" target="_blank" title="CSDN">
                    
                      <i class="fa fa-fw fa-globe"></i>CSDN</a>
                </span>
              
                <span class="links-of-author-item">
                  <a href="https://my.oschina.net/kangvcar/blog" target="_blank" title="OSChina">
                    
                      <i class="fa fa-fw fa-globe"></i>OSChina</a>
                </span>
              
                <span class="links-of-author-item">
                  <a href="mailto:kangvcar@126.com" target="_blank" title="E-Mail">
                    
                      <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                </span>
              
            
          </div>

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#环境要求："><span class="nav-number">1.</span> <span class="nav-text">环境要求：</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#环境搭建："><span class="nav-number">2.</span> <span class="nav-text">环境搭建：</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#1-安装-Ubuntu17-10-gt-安装步骤在这里"><span class="nav-number">2.0.0.0.1.</span> <span class="nav-text">1. 安装 Ubuntu17.10  > 安装步骤在这里</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#2-安装-Python2-7-14-Ubuntu17-10-默认Python版本为2-7-14"><span class="nav-number">2.0.0.0.2.</span> <span class="nav-text">2. 安装 Python2.7.14 (Ubuntu17.10 默认Python版本为2.7.14)</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#3-安装-git-、cmake-、-python-pip"><span class="nav-number">2.0.0.0.3.</span> <span class="nav-text">3. 安装 git 、cmake 、 python-pip</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#5-安装-face-recognition"><span class="nav-number">2.0.0.0.4.</span> <span class="nav-text">5. 安装 face_recognition</span></a></li></ol></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#实现人脸识别："><span class="nav-number">3.</span> <span class="nav-text">实现人脸识别：</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#示例一-1行代码实现人脸识别-："><span class="nav-number">3.0.1.</span> <span class="nav-text">示例一(1行代码实现人脸识别)：</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#1-首先你需要提供一个文件夹，里面是所有你希望系统认识的人的图片。其中每个人一张图片，图片以人的名字命名："><span class="nav-number">3.0.1.0.1.</span> <span class="nav-text">1. 首先你需要提供一个文件夹，里面是所有你希望系统认识的人的图片。其中每个人一张图片，图片以人的名字命名：</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#2-接下来，你需要准备另一个文件夹，里面是你要识别的图片："><span class="nav-number">3.0.1.0.2.</span> <span class="nav-text">2. 接下来，你需要准备另一个文件夹，里面是你要识别的图片：</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#3-然后你就可以运行face-recognition命令了，把刚刚准备的两个文件夹作为参数传入，命令就会返回需要识别的图片中都出现了谁："><span class="nav-number">3.0.1.0.3.</span> <span class="nav-text">3. 然后你就可以运行face_recognition命令了，把刚刚准备的两个文件夹作为参数传入，命令就会返回需要识别的图片中都出现了谁：</span></a></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#示例二-识别图片中的所有人脸并显示出来-："><span class="nav-number">3.0.2.</span> <span class="nav-text">示例二(识别图片中的所有人脸并显示出来)：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#示例三-自动识别人脸特征-："><span class="nav-number">3.0.3.</span> <span class="nav-text">示例三(自动识别人脸特征)：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#示例四-识别人脸鉴定是哪个人-："><span class="nav-number">3.0.4.</span> <span class="nav-text">示例四(识别人脸鉴定是哪个人)：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#示例五-识别人脸特征并美颜-："><span class="nav-number">3.0.5.</span> <span class="nav-text">示例五(识别人脸特征并美颜)：</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

<div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Kangvcar</span>

  
</div>


<div class="powered-by">
<i class="fa fa-user-md"></i><span id="busuanzi_container_site_pv">
    本站访客数:<span id="busuanzi_value_site_pv"></span>次
</span>
</div>








  <span class="post-meta-divider">|</span>




<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">博客全站共32.7k字</span>
</div>
        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.3"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.3"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.3"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.3"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.3"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.3"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.3"></script>



  


  

    
      <script id="dsq-count-scr" src="https://kangvcar.disqus.com/count.js" async></script>
    

    
      <script type="text/javascript">
        var disqus_config = function () {
          this.page.url = 'http://www.kangvcar.com/手把手教你用1行代码实现人脸识别-Python-Face-recognition/';
          this.page.identifier = '手把手教你用1行代码实现人脸识别-Python-Face-recognition/';
          this.page.title = '手把手教你用1行代码实现人脸识别 -- Python Face_recognition';
        };
        var d = document, s = d.createElement('script');
        s.src = 'https://kangvcar.disqus.com/embed.js';
        s.setAttribute('data-timestamp', '' + +new Date());
        (d.head || d.body).appendChild(s);
      </script>
    

  




	





  












<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src="//unpkg.com/valine/dist/Valine.min.js"></script>



  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  
  

  

  

  



  
    <script type="text/javascript" src="//cdn.bootcss.com/canvas-nest.js/1.0.0/canvas-nest.min.js"></script>
  
  
<!-- 页面点击小红心 -->
<script type="text/javascript" src="/js/src/love.js"></script>

</body>
</html>
